//Author: xiaruize
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, b, a) for (int i = (b); i >= (a); i--)
int testcase = 1, caseid;
const int N = 1e5 + 10;

int tot = 1;
int n, m, k;
int ori[N];
int qry[N], val[N];
vector<int> g[N];
int res[N];
int hsh[4];

int cnt[40];
struct node
{
    int l, r;
    int sum;
    bool flg;
    int lim;
    bool emp;
    int mx;
} tr[N << 2];

void build(int p, int l, int r, int dep)
{
    tr[p].l = l;
    tr[p].r = r;
    tr[p].sum = 0;
    tr[p].emp = true;
    tr[p].flg = 0;
    tr[p].lim = 0;
    tr[p].mx = 1e9;
    if (dep)
    {
        tr[p].flg = g[dep][(++cnt[dep])];
        tr[p].lim = dep;
    }
    // cerr << p << ' ' << l << ' ' << r << ' ' << dep << ' ' << tr[p].flg << ' ' << tr[p].lim << endl;
    if (l == r)
    {
        tr[p].sum = l;
        return;
    }
    int mid = l + r >> 1;
    build(p << 1, l, mid, dep - 1);
    build(p << 1 | 1, mid + 1, r, dep - 1);
    tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
}

bool upd(int p, int l, int r, int pos, int val, bool flg)
{
    if (l == r)
    {
        tr[p].emp = false;
        if (!flg)
        {
            tr[p].sum = 0;
            tr[p].mx = 0;
        }
        else
            tr[p].mx = val;
        return true;
    }
    int mid = l + r >> 1;
    bool cur = true;
    if (pos <= mid)
        cur &= upd(p << 1, l, mid, pos, val, flg && (tr[p].flg || tr[p].lim <= val));
    else
        cur &= upd(p << 1 | 1, mid + 1, r, pos, val, flg && (!tr[p].flg || tr[p].lim <= val) && (tr[p].flg || tr[p << 1].mx < tr[p].lim));
    tr[p].sum = tr[p << 1].sum + tr[p << 1 | 1].sum;
    tr[p].emp = (tr[p << 1].emp || tr[p << 1 | 1].emp);
    tr[p].mx = max(tr[p << 1].mx, tr[p << 1 | 1].mx);
    if (!tr[p].flg)
    {
        if (pos <= mid)
        {
            if (val >= tr[p].lim)
            {
                if (!tr[p << 1].emp)
                {
                    tr[p].emp = false;
                    tr[p].sum -= tr[p << 1 | 1].sum;
                }
            }
        }
    }
    else
    {
        if (pos > mid)
        {
            if (val >= tr[p].lim)
            {
                if (!tr[p << 1 | 1].emp)
                {
                    // cerr << "flag" << tr[p << 1].sum << endl;
                    tr[p].emp = false;
                    tr[p].sum -= tr[p << 1].sum;
                }
            }
        }
    }
    // cerr << pos << ' ' << val << ' ' << l << ' ' << r << ' ' << tr[p].emp << ' ' << tr[p].sum << ' ' << tr[p].lim << ' ' << tr[p].flg << endl;
    return true;
}

int getk(int x)
{
    return __lg(x) + (__builtin_popcount(x) != 1);
}

void solve()
{
    rep(i, 0, 3) cin >> hsh[i];
    rep(i, 1, n)
    {
        val[i] = (ori[i] ^ hsh[i % 4]);
        // cerr << val[i] << ' ';
    }
    // cerr << endl;
    // cerr << tot << endl;
    tot = 1;
    res[1] = 1;
    rep(i, 1, k)
    {
        memset(cnt, 0, sizeof(cnt));
        tot *= 2;
        build(1, 1, tot, i);
        rep(j, 1, (1 << i - 1)) upd(1, 1, tot, j, val[j], true);
        rep(j, (1 << i - 1) + 1, (1 << i))
        {
            upd(1, 1, tot, j, val[j], true);
            res[j] = tr[1].sum;
        }
    }
    int ans = 0;
    rep(i, 1, m)
    {
        ans ^= (i * res[qry[i]]);
        // cerr << res[qry[i]] << ' ';
    }
    cout << ans << endl;
}

signed main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m;
    rep(i, 1, n) cin >> ori[i];
    rep(i, 1, m) cin >> qry[i];
    k = getk(n);
    tot = (1 << k);
    // cerr << k << ' ' << tot << endl;
    rep(i, 1, k)
    {
        tot /= 2;
        g[i].clear();
        g[i].resize(tot + 1);
        rep(j, 1, tot)
        {
            char c;
            cin >> c;
            g[i][j] = c - '0';
        }
    }
    tot = 1;
    rep(i, 1, k) tot *= 2;
    cin >> testcase;
    // cerr << testcase << endl;
    while (testcase--)
        solve();
    return 0;
}
// g++ arena.cpp -o arena -std=c++14 -Wall -fsanitize=address,undefined -O2